<template>
  <div>
    <el-card class="box-card">
      <PageTools :totalNum="total">
        <!-- 新增按钮 -->
        <template #buttonPart>
          <el-button type="success" icon="el-icon-edit" size="medium" @click="addNewArt">新增文章</el-button>
        </template>
        <!-- 表单 -->
        <template #formPart>
          <el-form
            :inline="true"
            :model="formInline"
            class="form-inline"
            ref="formInline"
            :rules="rules">
            <el-form-item label="关键字" prop="keyword">
              <el-input v-model="formInline.keyword" placeholder="根据文章标题搜索"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="state">
              <el-select v-model="formInline.state" placeholder="请选择">
                <el-option label="已启用" value="1"></el-option>
                <el-option label="已禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="resetForm">清除</el-button>
              <el-button type="primary" @click="onSearch">搜索</el-button>
            </el-form-item>
          </el-form>
        </template>
      </PageTools>

      <!-- 表格：展示数据 -->
      <div class="table-content">
        <el-table
          v-loading="loading"
          :data="listData"
          style="width: 100%">
          <el-table-column
            align="center"
            type="index"
            label="序号"
            width="80">
          </el-table-column>
          <el-table-column
            align="center"
            label="文章标题"
            width="400">
            <template #default="{row}">
              <span>{{row.title}}</span>
              <i class="el-icon-film"
                style="color: #40aaff; cursor: pointer;"
                v-if="row.videoURL"
                @click="checkVideo(row)">
              </i>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="visits"
            label="阅读数"
            width="120">
          </el-table-column>
          <el-table-column
            align="center"
            prop="username"
            label="录入人"
            width="120">
          </el-table-column>
          <el-table-column
            align="center"
            label="录入时间"
            width="100">
            <template #default="{row}">
              <span>{{ row.createTime | timeTrans }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="状态"
            width="120">
            <template #default="{row}">
              <span v-if="row.state === 0">已禁用</span>
              <span v-else>已启用</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="操作">
            <template #default = "{row}">
              <el-button type="text" @click="lookDetail(row)">预览</el-button>
              <el-button type="text"  @click="updateState(row)">
                <span v-if="row.state === 0">启用</span>
                <span v-else>禁用</span>
              </el-button>
              <el-button type="text" :disabled="row.state===1" @click="updateArt(row.id)">修改</el-button>
              <el-button type="text" :disabled="row.state===1" @click="delArt(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <div class="page-divided">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="params.pagesize"
          layout="sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <DetailDialog
      :isPreview="isPreview"
      :visible.sync="visible"
      :artId="artId"
      :authName="authName">
    </DetailDialog>

    <AddEditDialog
      :isVisible.sync="isVisible"
      :isAdd="isAdd"
      :artId="artId"
      @success="getList(params)"
    >
    </AddEditDialog>

  </div>
</template>

<script>
import PageTools from '@/components/PageTools/index.vue'
import { getArticleList, updataArtState, deleteArticle } from '@/api/interview'
import AddEditDialog from './components/addEditDialog.vue'
import DetailDialog from './components/detailDialog.vue'
export default {
  name: 'InterviewSkill',
  components: {
    PageTools,
    AddEditDialog,
    DetailDialog
  },
  data() {
    return {
      loading: false,
      formInline: {
        keyword: '',
        state: '',
      },
      rules: {
        keyword:[],
        state: []
      },
      params: {
        page: 1,
        pagesize: 10
      },
      currentPage: 1,
      listData: [],
      total: 0,
      artstate: 0,
      isVisible: false,
      visible: false,
      artId: '',
      authName: '',
      isAdd: true,
      isPreview: true
    }
  },
  methods: {
    resetForm() {
      this.$nextTick(()=>{
        this.$refs.formInline.resetFields()
      })
    },
    onSearch() {
      if(!this.formInline.keyword && !this.formInline.state){
        this.getList(this.params)
      }else {
        this.getList({
          ...this.params,
          keyword: this.formInline.keyword + '',
          state: parseInt(this.formInline.state)
        })
      }
    },
    // 获取全部文章列表
    async getList(data) {
      this.loading = true
      const res = await getArticleList(data)
      this.total = res.counts
      this.listData = res.items
      this.loading = false
    },
    // 处理分页
    handleSizeChange(val) {
      this.params.pagesize = val
      if(this.formInline.keyword === '' && this.formInline.state === '') {
        this.getList(this.params)
      }else {
        this.onSearch()
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.params.page = val
      if(this.formInline.keyword === '' && this.formInline.state === '') {
        this.getList(this.params)
      }else {
        this.onSearch()
      }
    },
    // 修改文章状态
    async updateState(row) {
      if (row.state === 0){
        this.artstate = 1
      }else {
        this.artstate = 0
      }
      await updataArtState(row.id,this.artstate)
      this.getList(this.params)
    },
    // 查询文章详情
    lookDetail(row) {
      this.authName = row.username
      this.artId = row.id
      this.visible = true
      this.isPreview = true
    },
    // 删除文章
    delArt(id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await deleteArticle(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList(this.params)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 新增文章
    addNewArt() {
      this.isVisible = true
      this.artId = null
      this.isAdd = true
    },
    // 修改文章
    updateArt(id) {
      this.isVisible = true
      this.artId = id
      this.isAdd = false
    },
    // 查看视频
    checkVideo(row) {
      this.authName = ''
      this.isPreview = false
      this.artId = row.id
      this.visible = true
    }
  },
  created() {
    this.getList(this.params)
    this.resetForm()
  }
}
</script>

<style lang="scss" scoped>
.page-divided{
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
.table-content{
  margin-top: 20px;
}
</style>
